How do I loop through the running Excel instances

Hello,

to point on the running Excel.Application I use this command

oleGetAutoObject("Excel.Application")

How do I loop through the multiple running Excel instances to find the needed workbook?

Thank you.


Anta_st - Wed Sep 16 13:52:39 EDT 2015

Re: How do I loop through the running Excel instances
llandale - Wed Sep 16 19:05:58 EDT 2015

As far as I know you cannot loop through the various Excel.Applications; that command gets the "1st" one.

Since such an application can indeed have multiple workbooks you can loop through them.  Seems to me it is:

  • That command gets a "Application"
  • Get the "Documents" from the "Application"
  • loop for each "Document" in the "Documents"

-Louie

I suppose you CAN "loop" through the applications:

  • Get the 1st one.
  • if it is not the one you want then close all "Documents" therein and close the application
  • Repeat

Re: How do I loop through the running Excel instances
Anta_st - Thu Sep 17 02:53:37 EDT 2015

llandale - Wed Sep 16 19:05:58 EDT 2015

As far as I know you cannot loop through the various Excel.Applications; that command gets the "1st" one.

Since such an application can indeed have multiple workbooks you can loop through them.  Seems to me it is:

  • That command gets a "Application"
  • Get the "Documents" from the "Application"
  • loop for each "Document" in the "Documents"

-Louie

I suppose you CAN "loop" through the applications:

  • Get the 1st one.
  • if it is not the one you want then close all "Documents" therein and close the application
  • Repeat

Good morning Louie,

unfortunately to close other Excel applications isn't acceptable. User has to be able to work in Excel beside the tool which is communicating with DOORS.

So I need to handle the cases

1. No Excel applications are running, DOORS Export in Excel, User opens other Excel file => done,

2. Other Excel application is running, DOORS Export in new Excel instance -> here I don't know how to point the right Excel instance :(.

Regards,

 

 

 

 

Re: How do I loop through the running Excel instances
Mathias Mamsch - Thu Sep 17 03:13:21 EDT 2015

Anta_st - Thu Sep 17 02:53:37 EDT 2015

Good morning Louie,

unfortunately to close other Excel applications isn't acceptable. User has to be able to work in Excel beside the tool which is communicating with DOORS.

So I need to handle the cases

1. No Excel applications are running, DOORS Export in Excel, User opens other Excel file => done,

2. Other Excel application is running, DOORS Export in new Excel instance -> here I don't know how to point the right Excel instance :(.

Regards,

 

 

 

 

Hi,

as far as I know you have no possibility to access running Excel Instances using (DOORS) COM - only the one that was first started using oleGetAutoObject. You can however start and control as many excel instances as you like using oleCreateAutoObject.

From your post it seems you are already using oleCreateAutoObject. Good. Just store the handle to the Excel Application. If you are using a preexisting Excel library then chances are, that they store handles to workbook, application, etc. in global variables. If you need to run a second DXL script that accesses the results of the first script, you need to save the workbook at the end of the first script and open it again at the beginning of the second script.

If you are planning to use an excel process, that the user has started for an export, you should rethink your application design - you will never know the state of the excel instance of the user and there can always be a problem where you destroy data inside this instance.

Hope this helps, regards, Mathias

Re: How do I loop through the running Excel instances
Wolfgang Uhr - Thu Sep 17 09:02:58 EDT 2015

Hi

If you are using VBA, there is a possibility to select the correct Instance, but only if you knew a file which is loaded by this instance.

 

Set objApp = GetObject("C:\Users\thisUser\Desktop\Example.xlsx").Application
msgbox objApp.Name

 

But the oleGetAutoObject does not privide this parameter.

Best regards

Wolfgang

Re: How do I loop through the running Excel instances
Anta_st - Fri Sep 18 08:30:34 EDT 2015

Hello people,

thank you all for your comments and support :)!

I've solved the Topic in the following way:

1. I do not create extra instances in Excel and hence let exports be in the very first instance,

2. In case user opens an Excel file during the tool is running I provide the info that Excel Application cannot be used during the tool is running - Excel file goes hidden,

3. In case Excel application is already running -> start of tool, I hide the application fully (excepted the mask based on the DOORS Export),

4. All this I do with Excel Application Events.

(So actually I prohibit the user to work in Excel during the tool is running.)

Have a nice weekend!

 

Re: How do I loop through the running Excel instances
llandale - Sat Sep 26 15:46:20 EDT 2015

Anta_st - Fri Sep 18 08:30:34 EDT 2015

Hello people,

thank you all for your comments and support :)!

I've solved the Topic in the following way:

1. I do not create extra instances in Excel and hence let exports be in the very first instance,

2. In case user opens an Excel file during the tool is running I provide the info that Excel Application cannot be used during the tool is running - Excel file goes hidden,

3. In case Excel application is already running -> start of tool, I hide the application fully (excepted the mask based on the DOORS Export),

4. All this I do with Excel Application Events.

(So actually I prohibit the user to work in Excel during the tool is running.)

Have a nice weekend!

 

BTW, it is a bad idea to do other work while exporting, since the export typically copies-to-clipboard and then pastes-from-clipboard.  If the user is innocently copy-and-pasting it will mess up the export.